package com.vcredit.creditcat.service.product;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.dto.product.LoanExpendDto;
import com.vcredit.creditcat.dto.product.ProductDetailDto;
import com.vcredit.creditcat.model.MerchantProduct;
import com.vcredit.creditcat.model.ProductAssess;
import com.vcredit.creditcat.service.MerchantProductService;
import com.vcredit.creditcat.service.ProductAssessService;
import com.vcredit.framework.annotation.Log;

@Service
public class ProductService {
	
	private static final Logger LOG = LoggerFactory.getLogger(ProductService.class);
	
	private static final String MONTH = "月";
	private static final String DAY = "天";
	private static final String ZHONG_HUA_XIAN = "-";
	private static final String STR_1 = "个月";
	
	@Autowired
	private MerchantProductService merchantProductService;
	@Autowired
	private ProductAssessService productAssessService;

	@Log
	public ProductDetailDto detail(Long productId) {
		ProductDetailDto result = new ProductDetailDto();
		try {
			MerchantProduct merchantProduct = merchantProductService.findById(productId);
			if(null == merchantProduct) {
				return result;
			}
			String productApplycount = merchantProduct.getProductApplycount();
			Long loanCount = 0L;
			loanCount = StringUtils.isBlank(productApplycount) ? 0 : Long.parseLong(productApplycount);
			
			result.setProductId(merchantProduct.getProductId());
			result.setProductName(merchantProduct.getProductName());
			result.setProductIcon(merchantProduct.getProductIcon());
			result.setStar(merchantProduct.getStar());
			result.setFastestTime(merchantProduct.getFastestTime());
			result.setLoanCount(loanCount);
			result.setProductDesc(merchantProduct.getProductDesc());
			result.setApplyCondition(merchantProduct.getProductApplycondition());
			result.setJumpUrl(merchantProduct.getProductUri());
			
			LoanExpendDto loanExpend = new LoanExpendDto();
			List<Integer> limitList = this.getLimitList(merchantProduct.getLimitList());
			loanExpend.setMinAmt(merchantProduct.getProductMincredit() / 100);
			loanExpend.setMaxAmt(merchantProduct.getProductMaxcredit() / 100);
			loanExpend.setLimitList(limitList);
			loanExpend.setLimitUnit(merchantProduct.getLimitUnit());
			loanExpend.setMinRate(merchantProduct.getMinRate());
			loanExpend.setOtherExpend(merchantProduct.getOtherExpend());
			loanExpend.setLimitScope(this.getLimitScope(limitList,loanExpend.getLimitUnit()));
			
			result.setLoanExpend(loanExpend);
			
			List<ProductAssess> productAssessList = productAssessService.findByProductId(productId);
			result.setProductAssessList(productAssessList);
			
			return result;
		} catch (Exception e) {
			LOG.error("------------查询产品详情出错!!  productId=" + productId, e);
			throw e;
		}
	}
	
	private String getLimitScope(List<Integer> limitList, String limitUnit) {
		if(limitList.size() > 0) {
			StringBuilder sb = new StringBuilder();
			Integer minLimit = limitList.get(0);
			Integer maxLimit = limitList.get(limitList.size() - 1);
			if(MONTH.equals(limitUnit)) {
				if(minLimit == maxLimit.intValue()) {
					sb.append(minLimit).append(STR_1);
				} else {
					sb.append(minLimit).append(ZHONG_HUA_XIAN).append(maxLimit).append(STR_1);
				}
			}
			
			else {
				if(minLimit == maxLimit.intValue()) {
					sb.append(minLimit).append(DAY);
				} else {
					sb.append(minLimit).append(ZHONG_HUA_XIAN).append(maxLimit).append(DAY);
				}
			}
			return sb.toString();
			
		}
		return "";
	}

	private List<Integer> getLimitList(String limitListStr) {
		List<Integer> limitList = new ArrayList<Integer>();
		if(StringUtils.isBlank(limitListStr)) {
			return limitList;
		}
		String[] split = limitListStr.split("\\|");
		for(int i = 0; i < split.length - 1; i++) {
			for(int j = i + 1; j < split.length; j++) {
				int cur = Integer.parseInt(split[i].trim());
				int next = Integer.parseInt(split[j].trim());
				if(cur > next) {
					split[i] = next + "";
					split[j] = cur + "";
				}
			}
		}
		for(String str : split) {
			limitList.add(Integer.parseInt(str.trim()));
		}
		return limitList;
	}

}
